﻿using System;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class DefineSoundTag : Tag, IHasCharacterId
    {
        public ushort SoundId
        {
            get;
            protected set;
        }

        public AudioCodingFormat SoundFormat
        {
            get;
            protected set;
        }

        public SoundRates SoundRate
        {
            get;
            protected set;
        }

        public SoundSizes SoundSize
        {
            get;
            protected set;
        }

        public SoundTypes SoundType
        {
            get;
            protected set;
        }

        public UInt32 SoundSampleCount
        {
            get;
            protected set;
        }

        public SoundData SoundData
        {
            get;
            protected set;
        }

        public DefineSoundTag(int len, int tagOffset)
            : base(TagCodes.DefineSound, len, tagOffset)
        {

        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            long startIndex = reader.Position;

            SoundId = reader.ReadUInt16();

            SoundFormat = (AudioCodingFormat)reader.ReadUInt32(4);
            SoundRate = (SoundRates)reader.ReadUInt32(2);
            SoundSize = (SoundSizes)reader.ReadUInt32(1);
            SoundType = (SoundTypes)reader.ReadUInt32(1);

            reader.AlignBits();

            SoundSampleCount = reader.ReadUInt32();

            int soundLength = TagLength - (int)(reader.Position - startIndex);

            SoundData = SoundDataFactory.GetSoundData(SoundFormat, soundLength);

            SoundData.SwfDeserialize(reader);
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #region IHasCharacterID Members

        public ushort Id
        {
            get { return SoundId; }
        }

        #endregion
    }
}
